﻿using System;

namespace Framework.Library.Utilities
{
    public class ConvertUtility
    {
        public static string FormatTimeVn(DateTime dt, string defaultText)
        {
            if (ToDateTime(dt) != new DateTime(1900, 1, 1))
                return dt.ToString("dd-mm-yy");
            else
                return defaultText;
        }
        public static string FormatTimeVn(DateTime datetime)
        {
            string m = "";
            if (datetime.Minute > 9)
            {
                m = datetime.Minute.ToString();
            }
            else
            {
                m = "0" + datetime.Minute.ToString();
            }
            if(AppEnv.GetLanguage() == "vi-VN")
            {
                //return " Cập nhật : " + datetime.Hour + ":" + m + " - " + datetime.ToString("dd/MM/yyyy");
                return datetime.ToString("dd.MM.yyyy");

            }
            else
            {
                return " Update at : " + datetime.Hour + ":" + m + " - " + datetime.ToString("dd/MM/yyyy");
            }
                    
         }
        public static string ToStringWithoutFontBold(object obj)
        {
            string retVal;

            try
            {
                retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "").Replace("<span>","").Replace("</span>","");
            }
            catch
            {
                retVal = String.Empty;
            }

            return retVal;
        }
        public static string ToStringWithoutFontBold(object obj, int length)
        {
            string retVal;

            try
            {
                if (Convert.ToString(obj).Length > length)
                {
                    retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "").Substring(0, length).Replace("<span>", "").Replace("</span>", "");
                    retVal = retVal.Substring(0, retVal.LastIndexOf(" ")) + "..";
                }
                else
                {
                    retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "").Replace("<span>", "").Replace("</span>", "");
                }
            }
            catch
            {
                retVal = String.Empty;
            }

            return retVal;
        }
        private const string CONFIG_FILENAME = "/Modules/CMS/CMS.config";
        public static int LengthTeaser
        {
            get { return ToInt32(AppEnv.GetAppSetting("LengthTeaser", CONFIG_FILENAME)); }
        }
        
        public static string ToStringWithoutFontBoldLength(object obj, int length)
        {
            string retVal;

            try
            {
                if (length <= 30)
                {
                    retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "");
                    if(retVal.Length > LengthTeaser - 20)
                    {
                        retVal = retVal.Substring(0, LengthTeaser - 20);
                        retVal = retVal.Substring(0, retVal.LastIndexOf(" ")).Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("\n\r", "").Replace("\r\n", "").Replace("\"", "\"\"") + "..";
                    }
                }
                else if (length > 30 && length <=60)
                {
                    retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "");
                    if(retVal.Length > LengthTeaser - 60)
                    {
                        retVal = retVal.Substring(0, LengthTeaser - 65);
                        retVal = retVal.Substring(0, retVal.LastIndexOf(" ")).Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("\n\r", "").Replace("\r\n", "").Replace("\"", "\"\"") + "..";
                    }
                }
                else if (length > 60 && length <=90)
                {
                    retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "");
                    if(retVal.Length >  LengthTeaser - 100)
                    {
                        retVal = retVal.Substring(0, LengthTeaser - 107);
                        retVal = retVal.Substring(0, retVal.LastIndexOf(" ")).Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("\n\r", "").Replace("\r\n", "").Replace("\"", "\"\"") + "..";
                    }
                }
                else
                {
                    retVal = Convert.ToString(obj).Replace("<strong>", "").Replace("</strong>", "").Replace("<b>", "").Replace("</b>", "");
                    if(retVal.Length > LengthTeaser - 122)
                    {
                        retVal = retVal.Substring(0, LengthTeaser - 122);
                        retVal = retVal.Substring(0, retVal.LastIndexOf(" ")).Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("\n\r", "").Replace("\r\n", "").Replace("\"", "\"\"") + "..";
                    }
                }
                
            }
            catch
            {
                retVal = String.Empty;
            }

            return retVal;
        }
        public static int ToInt32(object obj)
        {
            int retVal = 0;

            try
            {
                retVal = Convert.ToInt32(obj);
            }
            catch
            {
                retVal = 0;
            }

            return retVal;
        }

        public static int ToInt32(object obj, int defaultValue)
        {
            int retVal = defaultValue;

            try
            {
                retVal = Convert.ToInt32(obj);
            }
            catch
            {
                retVal = defaultValue;
            }

            return retVal;
        }

        public static string ToString(object obj)
        {
            string retVal;

            try
            {
                retVal = Convert.ToString(obj);
            }
            catch
            {
                retVal = String.Empty;
            }

            return retVal;
        }

        public static DateTime ToDateTime(object obj)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj);
            }
            catch
            {
                retVal = DateTime.Now;
            }
            if (retVal == new DateTime(1, 1, 1)) return DateTime.Now;

            return retVal;
        }

        public static DateTime ToDateTime(object obj, DateTime defaultValue)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj);
            }
            catch
            {
                retVal = DateTime.Now;
            }
            if (retVal == new DateTime(1, 1, 1)) return defaultValue;

            return retVal;
        }

        public static bool ToBoolean(object obj)
        {
            bool retVal;

            try
            {
                retVal = Convert.ToBoolean(obj);
            }
            catch
            {
                retVal = false;
            }

            return retVal;
        }

        public static double ToDouble(object obj)
        {
            double retVal;

            try
            {
                retVal = Convert.ToDouble(obj);
            }
            catch
            {
                retVal = 0;
            }

            return retVal;
        }

        public static double ToDouble(object obj, double defaultValue)
        {
            double retVal;

            try
            {
                retVal = Convert.ToDouble(obj);
            }
            catch
            {
                retVal = defaultValue;
            }

            return retVal;
        }


    }
}